/**
 * author: Maxim Dybarskiy
 * date:   Jun 17, 2011
 * time:   3:26:48 PM
 */
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TimePicker;
import d.max.wallpaper.Service.WallpaperBinder;
import d.max.wallpaper.gui.TreeAdapter;
import d.max.wallpaper.gui.TreeView;
import d.max.wallpaper.util.TreeSelectionListener;
import java.io.File;

public class Activity extends android.app.Activity {

    public static final String LOG_PREFIX = "Random Wallpaper";
    private TreeSelectionListener listener;
    private WallpaperBinder binder;
    
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            binder = (WallpaperBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName cn) {
            binder = null;
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set content
        setContentView(R.layout.main);
        // set init configuration
        final EditText edit = (EditText) findViewById(R.id.path_value);
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence cs, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence cs, int i, int i1, int i2) {
                if (binder != null) {
                    binder.setPath(cs.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable edtbl) {
            }
        });
        TimePicker timePicker = (TimePicker) findViewById(R.id.time_value);
        timePicker.setIs24HourView(true);
        timePicker.setCurrentHour(6);
        timePicker.setCurrentMinute(0);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker tp, int hours, int minutes) {
                if (binder != null) {
                    binder.setDelay(hours, minutes);
                }
            }
        });
        listener = new TreeSelectionListener() {
            @Override
            public void pathSelected(String path) {
                edit.setText(path);
            }
        };
        TreeView treeView = (TreeView) findViewById(R.id.file_tree);
        treeView.setAdapter(new TreeAdapter(this));
        treeView.setListener(listener);
        treeView.browse(new File("/"));
        // start service
//        Intent intent = new Intent(Activity.this, Service.class);
//        startService(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // connect to service
//        Intent intent = new Intent(Activity.this, Service.class);
//        bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        // unbind service
//        unbindService(connection);
        super.onStop();
    }

    public TreeSelectionListener getListener() {
        return listener;
    }
}

